package com.henu.rescue.controller;

import com.henu.rescue.entity.Complain;
import com.henu.rescue.entity.VO.ComplainVO;
import com.henu.rescue.entity.VO.CostVO;
import com.henu.rescue.service.CostService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("cost/")
@Api("获取收费标准")
@Slf4j
public class CostController {

    @Autowired
    private CostService costService;

    @PostMapping("get")
    @ApiOperation("获取收费标准")
    public Result getCost(@RequestBody Page<CostVO> page) {
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page= costService.selectAllCost(page);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }

    @PostMapping("update")
    @ApiOperation("修改收费标准")
    public Result update(@RequestBody CostVO costVO) {
        costService.updateFee(costVO);
        return new Result(ResultEnums.SUCCESS);
    }
}
